package com.fm.openinstall.d;

import java.nio.*;
import java.nio.channels.*;
import java.io.*;
import java.util.*;

public class b
{
    private static int a(final FileChannel fileChannel, long n, final ByteBuffer byteBuffer) {
        int n2 = 0;
        while (byteBuffer.hasRemaining()) {
            final int read = fileChannel.read(byteBuffer, n);
            if (read == -1) {
                return n2;
            }
            n += read;
            n2 += read;
        }
        return n2;
    }

    private static int a(final FileChannel fileChannel, final long n, final byte[] array, int i, final int n2) {
        final ByteBuffer wrap = ByteBuffer.wrap(array, i, n2);
        int read;
        for (i = 0; i < n2; i += read) {
            read = fileChannel.read(wrap, i + n);
            if (read == -1) {
                return i;
            }
        }
        return i;
    }

    public static a a(final FileChannel fileChannel) {
        final h b = b(fileChannel);
        if (b == null) {
            return null;
        }
        if (b.f < 32L) {
            return new a(b);
        }
        final byte[] array = new byte[24];
        a(fileChannel, b.f - array.length, array, 0, array.length);
        final long c = com.fm.openinstall.d.c.c(array, array.length - 24, ByteOrder.LITTLE_ENDIAN);
        final long c2 = com.fm.openinstall.d.c.c(array, array.length - 16, ByteOrder.LITTLE_ENDIAN);
        final long c3 = com.fm.openinstall.d.c.c(array, array.length - 8, ByteOrder.LITTLE_ENDIAN);
        if (c2 != 2334950737559900225L || c3 != 3617552046287187010L) {
            return new a(b);
        }
        final int n = (int)(8L + c);
        final long f = b.f;
        final long n2 = n;
        final long n3 = f - n2;
        if (n < 32 || n3 < 0L) {
            return new a(b);
        }
        if (n2 > 20971520L) {
            return new a(b);
        }
        final ByteBuffer allocate = ByteBuffer.allocate(n - 24);
        allocate.order(ByteOrder.LITTLE_ENDIAN);
        if (a(fileChannel, n3, allocate) == allocate.capacity() && ((ByteBuffer)allocate.flip()).getLong() == c) {
            final f f2 = new f(n3);
            while (allocate.hasRemaining()) {
                final long long1 = allocate.getLong();
                final int int1 = allocate.getInt();
                final byte[] array2 = new byte[(int)(long1 - 4L)];
                allocate.get(array2, 0, array2.length);
                f2.a(int1, array2);
            }
            return new a(f2, b);
        }
        return new a(b);
    }

    private static void a(final FileChannel fileChannel, final FileChannel fileChannel2, long n, long n2) {
        while (n2 > 0L) {
            final long transferTo = fileChannel.transferTo(n, n2, fileChannel2);
            n += transferTo;
            n2 -= transferTo;
        }
    }

    public static void a(final byte[] array, File file, File file2) {
        while (true) {
            file = (File)new FileInputStream(file);
            file2 = (File)new FileOutputStream(file2);
            FileChannel channel;
            FileChannel channel2;
            Cloneable cloneable;
            f b;
            ByteBuffer[] e;
            int length;
            int n;
            long n2;
            ByteBuffer a;
            ByteBuffer byteBuffer = null;
            Label_0122_Outer:Label_0141_Outer:
            while (true) {
                while (true) {
                    while (true) {
                        try {
                            channel = ((FileInputStream)file).getChannel();
                            channel2 = ((FileOutputStream)file2).getChannel();
                            cloneable = a(channel);
                            channel.position(0L);
                            if (cloneable == null) {
                                a(channel, channel2, 0L, channel.size());
                                ((FileInputStream)file).close();
                                ((FileOutputStream)file2).close();
                                return;
                            }
                            ((a)cloneable).a(array);
                            b = ((a)cloneable).b();
                            cloneable = ((a)cloneable).a();
                            if (b != null) {
                                a(channel, channel2, 0L, b.b());
                                e = b.e();
                                length = e.length;
                                n = 0;
                                break Label_0251;
                            }
                            a(channel, channel2, 0L, ((h)cloneable).h);
                            // iftrue(Label_0266:, !byteBuffer.hasRemaining())
                            Block_6:
                            while (true) {
                                if (b != null) {
                                    n2 = b.a();
                                }
                                else {
                                    n2 = ((h)cloneable).f;
                                }
                                a = ((h)cloneable).a(n2);
                                while (a.hasRemaining()) {
                                    channel2.write(a);
                                }
                                ((FileInputStream)file).close();
                                ((FileOutputStream)file2).close();
                                return;
                                break Block_6;
                                a(channel, channel2, ((h)cloneable).f, ((h)cloneable).h - ((h)cloneable).f);
                                continue Label_0122_Outer;
                            }
                            channel2.write(byteBuffer);
                            continue Label_0141_Outer;
                        }
                        finally {
                            ((FileInputStream)file).close();
                            ((FileOutputStream)file2).close();
                            while (true) {}
                        }
                        if (n < length) {
                            byteBuffer = e[n];
                            continue Label_0141_Outer;
                        }
                        break;
                    }
                    continue;
                }
                Label_0266: {
                    ++n;
                }
                continue Label_0122_Outer;
            }
        }
    }

    private static h b(final FileChannel fileChannel) {
        final byte[] array = new byte[128];
        final long size = fileChannel.size();
        if (size < 22L) {
            return null;
        }
        long n;
        if (size > 65557L) {
            n = size - 65557L;
        }
        else {
            n = 0L;
        }
        for (long n2 = array.length - 22, n3 = size - array.length; n3 >= n - n2; n3 -= array.length - 22) {
            int n4;
            if (n3 < 0L) {
                n4 = (int)(-n3);
                Arrays.fill(array, 0, n4, (byte)0);
            }
            else {
                n4 = 0;
            }
            long n5;
            if (n3 < 0L) {
                n5 = 0L;
            }
            else {
                n5 = n3;
            }
            a(fileChannel, n5, array, n4, array.length - n4);
            for (int i = array.length - 22; i >= 0; --i) {
                if (array[i + 0] == 80 && array[i + 1] == 75 && array[i + 2] == 5 && array[i + 3] == 6) {
                    final int n6 = c.b(array, i + 20, ByteOrder.LITTLE_ENDIAN) & 0xFFFF;
                    final long h = n3 + i;
                    if (h + 22L + n6 == size) {
                        final h h2 = new h();
                        h2.h = h;
                        h2.a = (c.b(array, i + 4, ByteOrder.LITTLE_ENDIAN) & 0xFFFF);
                        h2.b = (c.b(array, i + 6, ByteOrder.LITTLE_ENDIAN) & 0xFFFF);
                        h2.c = (c.b(array, i + 8, ByteOrder.LITTLE_ENDIAN) & 0xFFFF);
                        h2.d = (0xFFFF & c.b(array, i + 10, ByteOrder.LITTLE_ENDIAN));
                        h2.e = ((long)c.a(array, i + 12, ByteOrder.LITTLE_ENDIAN) & 0xFFFFFFFFL);
                        h2.f = ((long)c.a(array, i + 16, ByteOrder.LITTLE_ENDIAN) & 0xFFFFFFFFL);
                        if (n6 > 0) {
                            h2.g = new byte[n6];
                            a(fileChannel, h2.h + 22L, h2.g, 0, n6);
                        }
                        return h2;
                    }
                }
            }
        }
        return null;
    }
}
